package com.sw.small_world.utils;



import com.sw.small_world.listener.UpLoadFileListener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;


/**
 * Created by zs on 2016/3/25.
 */
public class FileUtils {


    //包名目录
    public static final String PACKAGE_DIR = "/Android/data/com.huaching.www.parking/";
    //头像缓存目录
    public static final String USER_ICON_PATH = PACKAGE_DIR + "iconcache/";
    //拍摄头像上传拍照时文件存储的文件名
    public static final String USER_ICON_CAMERA_NAME = "user_icon_camera.jpg";
    //拍摄头像上传裁剪时文件存储的文件名
    public static final String USER_ICON_CUT_NAME = "user_icon_cut.jpg";
    //用户头像存储的文件名
    public static final String USER_ICON_FILE_NAME = "user_icon.jpg";


    /**
     * 临时创建一个文件用于保存调用相机拍照所获得的数据
     *
     * @return 临时存储图片
     */

    public static File getHeadPhotoCameraFile() {
        File file = new File(getHeadPhotoDir() + USER_ICON_CAMERA_NAME);
        return file;
    }

    /**
     * 存储裁剪后头像的文件
     *
     * @return 裁剪后图片
     */


    public static File getHeadPhotoCutFile() {
        File file = new File(getHeadPhotoDir() + USER_ICON_CUT_NAME);
        return file;
    }


    /**
     * 存储真正用户图像的文件
     *
     * @return 头像图片
     */

    public static File getHeadPhotoFile() {
        File file = new File(getHeadPhotoDir() + USER_ICON_FILE_NAME);
        return file;
    }



    /**
     * 获得用户头像文件夹路径
     *
     * @return path路径
     */
    public static String getHeadPhotoDir() {
        if (!SDCardUtils.isExsit()) {
            return null;
        }
        String path = SDCardUtils.getExternalStoragePath() + USER_ICON_PATH;
        SDCardUtils.mkdirs(path);
        return path;
    }


    /**
     * 获得用户裁剪后的头像的路径
     *
     * @return path路径
     */


    public static String getHeadPhotoCutPath() {
        return FileUtils.getHeadPhotoDir() + FileUtils.USER_ICON_CUT_NAME;
    }


    /**
     * 获得用户调用拍照或保存的照片的路径
     *
     * @return path路径
     */


    public static String getHeadPhotoCameraPath() {
        return FileUtils.getHeadPhotoDir() + FileUtils.USER_ICON_CAMERA_NAME;
    }


    /**
     * 获得用户头像路径
     *
     * @return path路径
     */

    public static String getHeadPhotoPath() {

        return FileUtils.getHeadPhotoDir() + FileUtils.USER_ICON_PATH;

    }


    /**
     * 复制文件方法
     *
     * @param source 源文件地址
     * @param target 目标文件地址
     */

    public static void copyFile(File source, File target) {
        FileChannel in = null;
        FileChannel out = null;
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        try {
            inStream = new FileInputStream(source);
            outStream = new FileOutputStream(target);
            in = inStream.getChannel();
            out = outStream.getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                inStream.close();
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     *
     * 上传文件的工具方法
     *
     *
     * @param path 文件路径
     * @param url 服务器地址
     * @param upLoadFileListener  上传的监听类
     * @throws Exception
     */


    public static void uploadFile(String path, String url, final UpLoadFileListener upLoadFileListener)  {


    }


    /**
     * 裁剪图片获得的文件名
     */

    public static String cutImageName(String path){
        String fileName = path.substring(path.lastIndexOf("=")+1);
        return fileName;
    }


}
